package com.winit.git.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.web.BaseController;
import com.winit.common.util.ObjectToString;
import com.winit.common.util.ShellRunUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

@RestController
@Api(value = "git", tags = {"git"}, description = "git")
@RequestMapping(value = "/api/v1/git")
public class GitController extends BaseController {
    static final Logger log = LoggerFactory.getLogger(GitController.class);

    @Value("${update.password}")
    private String update_password;
    @Autowired
    private Environment env;


    @RequestMapping(value = "/updateUI", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "")
    public
    @ResponseBody
    Map updateUI(@RequestBody JSONObject json) {
        log.info(ObjectToString.getString(json));
        ShellRunUtil.runCmds("source /etc/profile && update-matrix-ui ");
        return null;
    }

    @RequestMapping(value = "/updatePrototype", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "")
    public
    @ResponseBody
    Map updatePrototype(HttpServletRequest request, HttpServletResponse response) {
        ShellRunUtil.runCmds("source /etc/profile && update-matrix-prototype.sh ");
        return null;
    }

    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "")
    public
    @ResponseBody
    Map updateProduct(HttpServletRequest request, HttpServletResponse response) {
        ShellRunUtil.runCmds("source /etc/profile && update-matrix-product.sh dev_0415 ss");
        return null;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "")
    public
    @ResponseBody
    Map update(@RequestBody JSONObject json) {
        String password = json.getString("password");
        if (StringUtils.isEmpty(password) || !update_password.equals(password)) {
            log.error("密码错误.password=" + password);
            return null;
        }
//        2、判断是哪个应用
        JSONObject project = json.getJSONObject("project");
        String projectName = project.getString("name");

//       3、判断哪个分支
        String ref = json.getString("ref").replaceAll("refs/heads/", "");

        log.info("com.winit.git.web.GitController.update projectName=" + projectName + ",ref=" + ref);
        try {
            String listerCmd = env.getProperty("listen.project.cmd." + projectName);
            String listerBranch = env.getProperty("listen.project.branch." + projectName);
            log.info("com.winit.git.web.GitController.update listerCmd=" + listerCmd + ",listerBranch=" + listerBranch + ",projectName=" + projectName);
            if (StringUtils.isEmpty(listerCmd) || StringUtils.isEmpty(listerBranch)) {
                log.error("com.winit.git.web.GitController.update 配置文件为空，找不到对应的配置项 projectName=" + projectName);
                return null;
            }
            if (!ref.equals(listerBranch)) {
                log.info("com.winit.git.web.GitController.update 不是监听的分支，ref=" + ref + ",listerBranch=" + listerBranch + ",projectName=" + projectName);
                return null;
            }

            ShellRunUtil.runCmds("source /etc/profile && " + listerCmd.replaceAll("\\$\\{date}", String.valueOf(System.currentTimeMillis())));
        } catch (Exception e) {
            log.error("com.winit.git.web.GitController.update", e);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("update-matrix-ui ${date} master".replaceAll("\\$\\{date}", String.valueOf(System.currentTimeMillis())));
    }
}

